import { asyncRouterHandle } from "@/utils/asyncRouter";
// import getMenuData from "../getMenu.json";
import { asyncMenu } from "@/api/systemManage/menu";

const formatRouter = (routes, routerList) => {
  return (
    routes &&
    routes.forEach((item) => {
      if (
        (!item.children || item.children.every((ch) => ch.hidden)) &&
        item.name != "404"
      ) {
        routerList.push({
          label: item.meta.title,
          value: item.name,
          buttons: item.buttons,
        });
      }
      if (item.children && item.children.length > 0) {
        formatRouter(item.children, routerList);
      }
    })
  );
};

export const router = {
  namespaced: true,
  state: {
    asyncRouters: [],
    routerList: [],
  },
  mutations: {
    setRouterList(state, routerList) {
      state.routerList = routerList;
    },
    // 设置动态路由
    setAsyncRouter(state, asyncRouters) {
      state.asyncRouters = asyncRouters;
    },
  },
  actions: {
    // 从后台获取动态路由
    async SetAsyncRouter({ commit }) {
      const baseRouter = [
        {
          path: "/layout",
          name: "layout",
          component: "view/layout/index.vue",
          meta: {
            title: "底层layout",
          },
          children: [],
        },
      ];
      const asyncRouterRes = await asyncMenu({});
      // const asyncRouterRes = getMenuData
      if (asyncRouterRes.code != 0) {
        return;
      }
      const asyncRouter = asyncRouterRes.data || [];
      asyncRouter.push({
        path: "404",
        name: "404",
        hidden: true,
        meta: {
          title: "",
        },
        component: "view/error/index.vue",
      });
      // 重置routerList
      const routerList = [];
      formatRouter(asyncRouter, routerList);
      baseRouter[0].children = asyncRouter;
      // baseRouter.push({
      //   path: "*",
      //   redirect: "/layout/404",
      // });
      asyncRouterHandle(baseRouter);
      commit("setAsyncRouter", baseRouter);
      commit("setRouterList", routerList);
      return true;
    },
  },
  getters: {
    // 获取动态路由
    asyncRouters(state) {
      return state.asyncRouters;
    },
    routerList(state) {
      return state.routerList;
    },
    defaultRouter(state) {
      return state.defaultRouter;
    },
  },
};
